package net.zjitc.service.Impl;

import net.zjitc.dao.CartDao;
import net.zjitc.dao.Impl.CartDaoImpl;
import net.zjitc.dao.Impl.OrderDaoImpl;
import net.zjitc.dao.OrderDao;
import net.zjitc.pojo.Orders;
import net.zjitc.service.OrderService;
import net.zjitc.utils.db.C3P0Utils;

import java.sql.SQLException;

/**
 * Created by Administrator on 2017/12/14.
 */
public class OrderServiceImpl implements OrderService {
    OrderDao orderDao = new OrderDaoImpl();
    CartDao cartDao = new CartDaoImpl();
    public int add(Orders orders) {
        //插入一条订单（订单，订单条目）
        //开始事务
        //执行sql
        //提交事务
        //如果有错，将回滚
        try {
            C3P0Utils.startTransation();

            //购物车清空与订单生成 看成一个业务 要同时操作成功才可以  只要一个没有成功，则回滚
            orderDao.add(orders);
            cartDao.clearCart(orders.getUser().getUid());

            C3P0Utils.commitTransation();
        } catch (SQLException e) {
            try {
                C3P0Utils.rollbackTransatin();

            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
        return 0;
    }
}
